$(function () {
    mui.init();
    var req = {
        'dtid': localStorage.getItem('dtid'),
        'userid': localStorage.getItem('userid'),
        'token': localStorage.getItem('token')
    }
    mui.ready(function () {
        //加载框
        mui.showLoading("正在加载..", ".warn-list");
        Request();
        
        mui('.report-list').on('tap', 'li a', function () {
            var fileUrl=$(this).attr('data-file');
            var fileType= fileUrl.split('.')[fileUrl.split('.').length - 1] ;
            // alert(fileUrl);
            var html = '';

            if( fileType == 'jpg' ){
                 html = `
                <img src="${fileUrl}" alt="">
                `;
            } else if(fileType == 'pdf'){
                PDFObject.embed(fileUrl, ".wenjian",options);
            } else{
                html = `
                <a href="${fileUrl}">点击下载该文件</a>
                `;
            }
            $('.wenjian').html(html);
            $('.monitor_det').fadeIn();

        })
        mui('.monitor_det').on('tap', '.full-close', function () {
          $(this).parent().fadeOut(); 
        })
        
    });
    //ajax请求处理数据
    function Request() {
        var url = domain + "/dtwbgl/ycreport.h5";
        $.ajax(url, {
            // data: JSON.stringify(req),
			data:req,
            dataType: 'json', //服务器返回json格式数据
            type: 'post', //HTTP请求类型
            timeout: 20000, //超时时间
            async: true, //true异步加载
			 headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            success: function (data) {
                if (data.code == 2001) {
                    //返回值处理
                    var ref = data.data;
                    console.log(data);
                   
                    var comref = ref.sort(compare('yearcheck')); //排序

                    var html='';
                    comref.forEach(function(e,i){
                        var wjname=e.name;
                        var u=e.url;
                    //  这里拼接成年检报告的路径,在项目机器上的/sign/ycfile下                      
                        var u="http://"+configureIp+"/sign/ycfile/"+e.url;
                        html+=`
                        <li class="mui-table-view-cell" >
                            <a href='javascript:;' data-file="${u}" class="mui-navigate-right">${wjname}</a>
                        </li> 
                        `;
                    })
                    $('.report-list').append(html);


                    //隐藏加载框
                    mui.hideLoading(function () { //隐藏后的回调函数  
                    });
                } else if (data.code == 2002) {
                    mui.alert(data.msg);
                    mui.openWindow({
                        url: '../../login.html',
                        id: 'login.html'
                    })
                } else {
					mui.alert(data.msg);
                    mui.openWindow({
                    		url: '404.html',
                    		id: '404.html'
                    })
                }
            },
            error: function (xhr, type, errorThrown) { //异常处理； console.log(type);
               mui.alert(type);
               mui.openWindow({
               	url: '../../login.html',
               	id: 'login.html'
               })
            }
        });
    }

    //排序
    function compare(property) {
        return function (a, b) {
            var value1 = a[property];
            var value2 = b[property];
            return Date.parse(value1) - Date.parse(value2);
        }
    }

})